//
//  ViewController.m
//  时间
//
//  Created by cjy on 16/3/21.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "ViewController.h"
#import "NetworkHandeler.h"
#import "FirstTableViewCell.h"
#import "Model.h"
#import <SDWebImageManager.h>


@interface SDWebImageManager  (cache)


- (BOOL)memoryCachedImageExistsForURL:(NSURL *)url;

@end


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,NetworkHandelerDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *array;

@end

@implementation SDWebImageManager (cache)

- (BOOL)memoryCachedImageExistsForURL:(NSURL *)url {
    NSString *key = [self cacheKeyForURL:url];
    return ([self.imageCache imageFromMemoryCacheForKey:key] != nil) ?  YES : NO;
}

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self handle];
    
    [self createtableView];
    
}

- (void)handle{

    NSString *str = @"http://api.vmovie.app887.com/api/Articles.action?keyword=&npc=0&opc=20&type=精选&uid=7912";
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];


}

- (void)handelerDidComplete:(id)result{

    self.array = [NSMutableArray array];
    NSDictionary *dic = [result objectForKey:@"root"];
    
    
    NSArray *arrlist = [dic objectForKey:@"list"];
    
    for (NSDictionary *diclist in arrlist) {
        
        Model *model = [[Model alloc]init];
        model.imglink = [diclist objectForKey:@"imglink"];
        model.title = [diclist objectForKey:@"title"];
        
    
        [self.array addObject:model];
        NSLog(@"%ld",self.array.count);
     
        
    }
    
    [self.tableView reloadData];

}


- (void)createtableView{

    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[FirstTableViewCell class] forCellReuseIdentifier:@"pool"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
//    
//    
//    Model *model = self.array[indexPath.row];
//    
//    [cell passModel:model];

    return cell;
    


}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    NSArray<FirstTableViewCell *> *array = [self.tableView visibleCells];
    
    [array enumerateObjectsUsingBlock:^(FirstTableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [obj cellOffset];
    }];
    
    
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(FirstTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    Model *model = self.array[indexPath.row];
    [cell passModel:model];
    
    if (![[SDWebImageManager sharedManager] memoryCachedImageExistsForURL:[NSURL URLWithString:model.imglink]]) {
        
        CATransform3D rotation;//3D旋转
        
        rotation = CATransform3DMakeTranslation(0 ,50 ,20);

        //逆时针旋转
        
        rotation = CATransform3DScale(rotation, 0.9, .9, 1);
        
        rotation.m34 = 1.0/ -600;
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.layer.shadowOffset = CGSizeMake(10, 10);
        cell.alpha = 0;
        
        cell.layer.transform = rotation;
        
        [UIView beginAnimations:@"rotation" context:NULL];
        //旋转时间
        [UIView setAnimationDuration:0.6];
        cell.layer.transform = CATransform3DIdentity;
        cell.alpha = 1;
        cell.layer.shadowOffset = CGSizeMake(0, 0);
        [UIView commitAnimations];
    }
    
    [cell cellOffset];
//    cell.model = model;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 250;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
